package com.homegoods.classify.widget;

import android.content.Context;
import android.graphics.Rect;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.homegoods.classify.R;
import com.homegoods.classify.model.GoodsModel;
import com.kedll.library.adapter.MultiTypeAdapter;
import com.kedll.library.mvp.BaseItem;
import com.kedll.library.util.BindingUtil;
import com.kedll.library.util.MyUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author QiQi
 */
public class ClassifyHeadView extends LinearLayout {

    View searchBar;
    ImageView convenientBanner;
    RecyclerView hotGoodsList;

    /**
     * 全场热销适配器
     */
    MultiTypeAdapter hotGoodsAdapter;


    public ClassifyHeadView(Context context) {
        this(context, null);
    }

    public ClassifyHeadView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ClassifyHeadView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.classify_head_layout, this, true);
        init(context);
    }

    private void init(Context context) {
        searchBar = findViewById(R.id.search_bar);
        convenientBanner = findViewById(R.id.convenientBanner);
        hotGoodsList = findViewById(R.id.gold_medal_list);


        LinearLayoutManager layoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
        hotGoodsList.setHasFixedSize(true);
        hotGoodsList.setLayoutManager(layoutManager);

        hotGoodsAdapter = new MultiTypeAdapter();

        hotGoodsList.setAdapter(hotGoodsAdapter);


    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        hotGoodsAdapter.clear();

    }


    /**
     * 设置金牌栏目推荐数据
     *
     * @param items
     */
    public void setGoodsItem(List<GoodsModel> items) {

        List<BaseItem> goldMedalColumnItems = new ArrayList<>();
        for (GoodsModel model : items) {
            goldMedalColumnItems.add(model.createItem(hotGoodsAdapter));
        }

        hotGoodsAdapter.addAllWithClear(goldMedalColumnItems);
    }


    /**
     * 设置广告
     */
    public void setAdList(String images) {
        BindingUtil.loadImageNoTransform(convenientBanner, images, null, null);
    }

    /**
     * 设置RecycleView间的间距
     */
    private final class SpacesItemDecoration extends RecyclerView.ItemDecoration {
        private int space;

        public SpacesItemDecoration() {
            space = MyUtils.dp2px(10);
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            int position = parent.getChildLayoutPosition(view);
            if (position % 2 == 0) {
                if (position == 0) {
                    outRect.set(0, 0, space / 2, space / 2);
                } else {
                    outRect.set(0, space / 2, space / 2, space / 2);
                }
            } else if ((position & 1) == 1) {
                if (position == 1) {
                    outRect.set(space / 2, 0, 0, space / 2);
                } else {
                    outRect.set(space / 2, space / 2, 0, space / 2);
                }
            }
        }
    }

}